// pages/evaluate/evaluate.js
var util = require('../../utils/util.js')
Page({
  data:{
    evaluateStatus: [{status:'3', title:'好评'}, {status:'2', title:'中评'}, {status:'1', title:'差评'}],
    listData:[],
    isNoName: false
  },
  onLoad:function(options){
    let order = wx.getStorageSync('evaluateData')
    this.masterOrderId = order.orderId
    wx.removeStorageSync('evaluateData')
    this.structureData(order)
  },
  onReady:function(){
    // 页面渲染完成
  },
  onShow:function(){
    // 页面显示
  },
  onHide:function(){
    // 页面隐藏
  },
  onUnload:function(){
    // 页面关闭
  },
  structureData:function(order) {
    let listData = []
    for (let order of order.goodsList) {
      let evaluateDic = {}
      evaluateDic.evaluationType = '1'//0:订单，1:商品
      evaluateDic.evaluationGrade = '3'
      evaluateDic.tradeOrderId = order.tradeOrderId //子订单ID;
      if (order.suitId) {
        evaluateDic.suiteId = order.suitId
      } else {
        evaluateDic.goodsIdOrDesignCode = order.goodsIdOrDesignCode
      }
      evaluateDic.isCustom = order.isCustom
      evaluateDic.goodsName = order.goodsName
      evaluateDic.materialCode = order.materialCode
      evaluateDic.goodsImg = order.goodsImg
      evaluateDic.imageUrl =  []
      evaluateDic.isAnonymous = "1"  //0:匿名， 1：否
      evaluateDic.evaluationContent = ''
      listData.push(evaluateDic)
    }
    this.setData({listData:listData})
  },
  inputEnd:function(event) {
    let tradeOrderId = event.currentTarget.id
    for (let evaluateDic of this.data.listData) {
      if (evaluateDic.tradeOrderId == tradeOrderId) {
        evaluateDic.evaluationContent = event.detail.value
        break;
      }
    }
  },
  clickStatusItem:function(event) {
    let tradeOrderId = event.currentTarget.id
    let listData = this.data.listData
    for (let evaluateDic of listData) {
      if (evaluateDic.tradeOrderId == tradeOrderId) {
        evaluateDic.evaluationGrade = event.currentTarget.dataset.status
        break
      }
    }
    this.setData({listData:listData})
  },
  uploadPic:function(event) {
    let tradeOrderId = event.currentTarget.id
    let listData = this.data.listData
    let that = this
    for (let evaluateDic of listData) {
      if (evaluateDic.tradeOrderId == tradeOrderId) {
        let token = getApp().globalData.mohuanUser["X-AUTH-TOKEN"]
        wx.chooseImage({
            count: 1, // 默认9
            sizeType: 'compressed', // 可以指定是原图还是压缩图，默认二者都有
            success: function (res) {
                // 返回选定照片的本地文件路径列表，tempFilePath可以作为img标签的src属性显示图片
                var tempFilePaths = res.tempFilePaths
                wx.showToast({ title: 'loading..', icon: 'loading', uration: 20000})
                wx.uploadFile({
                url: 'https://www.magicmanufactory.com/c2mwebservice/api/v2/test/WXSmartProgram',
                filePath:tempFilePaths[0],
                name:'picFile',
                header: {'Accept': 'application/json',
                          'C2M-Identify':'Platform:iOS,APP Version:6.300,OS Version:10.2,Device Type:xiaochengxu',
                          'Content-Encoding':'gzip',
                          'X-AUTH-TOKEN': token
                        }, // 设置请求的 header
                formData: {id:"comment"}, // HTTP 请求中其他额外的 form data
                success: function(res){
                  wx.hideToast()
                  let data = JSON.parse(res.data)
                  let url = data.returnData[0]
                  evaluateDic.imageUrl.push(url)
                  that.setData({listData:listData})
                    // success
                },
                fail: function(res) {
                  wx.hideToast()
                    // fail
                },
                complete: function(res) {
                    // complete
                }
                })
            }
        })
        break
      }
    }
  },

  deleteItem:function(event) {
    let tradeOrderId = event.currentTarget.id
    let listData = this.data.listData
    let index = event.currentTarget.dataset.index
    for (let evaluateDic of listData) {
      if (evaluateDic.tradeOrderId == tradeOrderId) {
        evaluateDic.imageUrl.splice(index, 1)
      }
    }
    this.setData({listData:listData})
  },
  clickNoName:function(event) {
    this.setData({isNoName:!this.data.isNoName})
    for (let evaluateDic of this.data.listData) {
      evaluateDic.isAnonymous = this.data.isNoName ? "0" : "1"  //0:匿名， 1：否
    }
  },
  sendEvaluate:function(event) {
    var that = this;
    wx.showToast({ title: 'loading..', icon: 'loading', uration: 20000})
    util.postRequest(`/c2mwebservice/api/v1/evaluationGoods/${getApp().globalData.mohuanUser.memberId}/${this.masterOrderId}`, this.data.listData, function(res) {
        wx.hideToast()
        let returnData = res.returnData
    }, function(res) {
      wx.hideToast()
      console.log(res);
    })
  }
})
